package com.tianji.learning.controller;

import com.tianji.common.domain.dto.PageDTO;
import com.tianji.common.domain.query.PageQuery;
import com.tianji.learning.domain.dto.LearningPlanDTO;
import com.tianji.learning.domain.vo.LearningLessonVO;
import com.tianji.learning.domain.vo.LearningPlanPageVO;
import com.tianji.learning.domain.vo.LearningStatusVO;
import com.tianji.learning.service.ILearningLessonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @Description LearningLessonController
 * @Author zhd
 * @Date 2024-12-07
 */
@Api(tags = "我的课表相关接口")
@RestController
@RequestMapping("/lessons")
@RequiredArgsConstructor
@Slf4j
public class LearningLessonController {
	@Autowired
	private ILearningLessonService learningLessonService;

	@ApiOperation("查询我的课表，排序字段 latest_learn_time:学习时间排序，create_time:购买时间排序")
	@GetMapping("/page")
	public PageDTO<LearningLessonVO> queryMyLessons(PageQuery pageQuery) {
		return learningLessonService.queryMyLessons(pageQuery);
	}

	@GetMapping("/now")
	@ApiOperation("查询当前正在学习的课程")
	public LearningLessonVO queryNowLearningLesson() {
		return learningLessonService.queryMyCurrentLesson();
	}

	@DeleteMapping("/{courseId}")
	@ApiOperation("删除失效的课程")
	public void cancelLearning(@PathVariable("courseId") Long courseId) {
		learningLessonService.removeByCId(courseId);
	}

	/**
	 * 校验当前用户是否可以学习当前课程
     * @param courseId 课程id
	 * @return lessonId，如果是报名了则返回lessonId，否则返回空
	 */
	@GetMapping("/{courseId}/valid")
	@ApiOperation("检查课程是否有效")
	public Long isLessonValid(@PathVariable("courseId") Long courseId) {
		return learningLessonService.isLessonValid(courseId);
	}

	@GetMapping("{courseId}")
	@ApiOperation("查询用户课表中指定课程状态")
	public LearningStatusVO queryLearningStatus(@PathVariable("courseId") Long courseId) {
		return learningLessonService.queryLearningStatus(courseId);
	}

	/**
	 * 统计课程学习人数
	 * @param courseId 课程id
	 * @return 学习人数
	 */
	@GetMapping("/lessons/{courseId}/count")
	public Integer countLearningLessonByCourse(@PathVariable("courseId") Long courseId){
		return learningLessonService.countLearningLessonByCourse(courseId);
	}

	/**
	 * 查询我的学习计划
	 *
	 * @param query
	 * @return
	 */
	@GetMapping("/plans")
	@ApiOperation("查询我的学习计划")
	public LearningPlanPageVO queryMyPlans(PageQuery query) {
		log.info("查询我的学习计划 请求参数：{}", query);
		return learningLessonService.queryMyPlans(query);
	}

	@ApiOperation("创建学习计划")
	@PostMapping("/plans")
	public void createLearningPlans(@Valid @RequestBody LearningPlanDTO planDTO) {
		learningLessonService.createLearningPlan(planDTO.getCourseId(), planDTO.getFreq());
	}
}
